;;;; -*- mode: lisp -*-
;;;;
;;;; $Id: $
;;;;
;;;; Testbed for NCBI eutils 
;;;;
;;;; Copyright (C) 2007 Utz-Uwe Haus. All Rights Reserved.
;;;;
;;;; You are granted the rights to distribute and use this software
;;;; as governed by the terms of the Lisp Lesser GNU Public License
;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
;;;;

(in-package :cl-soap)

;; URL of the wsdl file we have to parse
(defvar +eutils-wsdl-url-base+ "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/soap/")
(defvar +eutils-wsdl-url+ (concatenate 'string +eutils-wsdl-url-base+ "eutils.wsdl"))
;; directory containing the downloaded copy (change apropriately)
(defvar +eutils-wsdl-dir+ "test/xml-files/")
(defvar +eutils-wsdl-file+ (concatenate 'string +eutils-wsdl-dir+ "eutils.wsdl"))

;; A helper function to retrieve the schemas referenced by the wsdl file
;; Note: The WSDL or XMLSchema spec do not specify that SCHEMA-LOCATION is
;; supposed to be a URL inside the <xsd:schema> element.
;; In fact, they leave that deliberately open (sec 4.2.3 in xmlschema).
(defun eutils-include-url (schema-location)
  "Retrieve the file SCHEMA from the NCBI eutils repository."
  (wsdl-include-url
   (concatenate 'string
		+eutils-wsdl-url-base+ schema-location)))

(defun eutils-include-file (schema-location)
  "Retrieve the file SCHEMA from the local copy of the NCBI eutils repository."
  (wsdl-include-file
   (concatenate 'string
		+eutils-wsdl-dir+ schema-location)))

;; Parse things
(defun eutils-parse-wsdl (&key (file +eutils-wsdl-file+) (url +eutils-wsdl-url+) verbose)
  "Parse eutils wsdl from FILE or URL. If VERBOSE, show a textual description of
the parse result (on *standard-output*)."
  (let ((result (if file
		    (parse-wsdl-file file 
				     :import #'eutils-include-file
				     :include #'eutils-include-file
				     )
		    (parse-wsdl-url url
				    :import #'eutils-include-url
				    :include #'eutils-include-url))))
    (when verbose
      (describe-wsdl-soap result))
    result))

(defun eutils-e-fetch-request (&key (db "pubmed") (id "10708388") ;; and more
			       )
  (let ((wsdl-obj (parse-wsdl-file +eutils-wsdl-file+ 
				   :import #'eutils-include-file
				   :include #'eutils-include-file
				   )))
    (wsdl-soap-call wsdl-obj 
		    "run_eFetch"
		    :input `("eFetchRequest" ("db" ,db "id" ,id "tool" "cl-soap")))))

;;;; eof
